import employee from '@/api/system/employeeAPI'

export default {
  data () {
    return {
      tableId: '', // 表ID
      loading: false, // 加载
      searchParams: {}, // 查询条件参数
      list: [], // 数据集合
      totalRow: 0, // 总行数
      pageSize: 10, // 每页行数
      currentPage: 1, // 当前页,
      employees: [], // 负责员工
      productTypes: []// 产品类型
    }
  },
  methods: {
    initPageList () {
      this.pageList({currentPage: this.currentPage, pageSize: this.pageSize})
    },
    /**
     * 分页查询
     * @param pageInfo
     */
    pageList (pageInfo) {
      const requestAPI = this.getRequestType()
      if (!requestAPI) {
        return
      }
      const params = {...this.searchParams}
      params.pageNumber = pageInfo.currentPage
      params.pageSize = pageInfo.pageSize
      // console.log('请求参数：', params)
      this.loading = true
      const vm = this
      requestAPI(params).then(res => {
        setTimeout(() => {
          vm.loading = false
          vm.list = res.data.list || []
          vm.totalRow = res.data.totalRow
        }, 1000)
        vm.$nextTick(() => {
          // 滚动条 回到初始状态
          document.querySelector('.el-table__body-wrapper').scrollTop = 1
        })
      }).catch(error => {
        console.log(error)
        vm.loading = false
      })
    },
    /**
     * 分页查询 条件查询
     * @param searchParams
     */
    search (searchParams) {
      this.searchParams = searchParams
      this.initPageList()
    },
    /**
     *  请求
     * @param opt  add:新增 update:修改 delete:删除
     * @param params
     * @param successBack
     * @param errorBack
     */
    requestOption (opt, params, successBack, errorBack) {
      const requestAPI = this.getRequestType(opt)
      if (!requestAPI) {
        return
      }
      requestAPI(params).then(res => {
        if (successBack && successBack !== null) {
          successBack(res)
        }
      }).catch(error => {
        if (errorBack && errorBack !== null) {
          errorBack(error)
        }
      })
    },
    /**
     *  校验表单
     * @param formName
     * @returns {boolean}
     */
    checkFormData (formName) {
      let f = false
      this.$refs[formName].validate(valid => {
        f = valid
      })
      return f
    },
    /**
     *  查询员工 (过滤)
     * @param queryString
     * @param cb
     */
    getFilterEmployee (queryString, cb) {
      employee.getEmployeeListAPI().then((res) => {
        this.employees = res.data
        let employees = this.employees
        let results = queryString
          ? employees.filter((emp) => {
            return (
              emp.employeeName
                .toLowerCase()
                .indexOf(queryString.toLowerCase()) !== -1)
          })
          : employees
        cb(results)
      })
    },
    /**
     * 查询产品类型
     * @returns {*[]}
     */
    getProductTypeInfo () {
      return []
    }

  }
}
